/*
 * 文件名：TestLogin.java
 * 版权：Copyright by www.suyin.com
 * 描述：登录测试接口类
 * 修改人：cqm
 * 修改时间：2013-1-16
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.caiyouxi.pay.payment.weixin.wxpay;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * 测试校验版本接口类
 * 
 * @author hj
 * @version 2013-1-16
 * @see TestPay
 * @since
 */
public class TestPay extends Thread
{
    HttpURLConnection httpConn = null;

    InputStream is = null;

    BufferedReader rd = null;

    public void send()
    {

        try
        {
            String appKey = "INiiwer85UixhX350A55p3dG531Eb1jrOHc2syY2x5uzVLnCjjERMFlqv5N8zkcmYrea0JLT9Uriy1dEeL9KyEDJDHV6ovNQKSZPvihqvUZ2fOcpkCR7RFmWpcp5ZxxS";
            //微信请求加密比对签名
            String appSignCompare = "appid=wx87c069f23574919d&appkey="
                                    + appKey
                                    + "&deliver_msg=ok&deliver_status=1&deliver_timestamp=1397218272&openid=ovQVvuLxIKj4AKKO-wo-zyltwTtQ&out_trade_no=Hu5VV5HqVF5vWcuF&transid=1218456501201404113173608755";
            //加密
            appSignCompare = SHA1Util.Sha1(appSignCompare);

            String accessToken = "KqMdz8i190B-_uR_InS1RNRdibzBNA-5WtvD9VzOWQfo8QeUXBGgt3OVulEf3lmqGTWNt6ExYW9sZ0sJ6HjhaMHHpqvHo0VGVv64p0ZuXSCuQE3REHbEyRLMvnbySgixajymBsKZ4uCUz9WuMfxM0w";
            String xmlData = "{'appid' : 'wx87c069f23574919d','openid' : 'ovQVvuLxIKj4AKKO-wo-zyltwTtQ','transid' : '1218456501201404113173608755','out_trade_no' : 'Hu5VV5HqVF5vWcuF','deliver_timestamp' : '1397218272','deliver_status' : '1','deliver_msg' : 'ok','app_signature' : '"
                             + appSignCompare + "','sign_method' : 'sha1'}";

            URL url = new URL("https://api.weixin.qq.com/pay/delivernotify?access_token="
                              + accessToken);
            httpConn = (HttpURLConnection)url.openConnection();
            httpConn.setRequestProperty("Host", "https://api.weixin.qq.com");
            httpConn.setRequestProperty("Content-Length", String.valueOf(xmlData.length()));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=\"UTF-8\"");
            httpConn.setRequestMethod("POST");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            OutputStream out = httpConn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
            osw.write(xmlData);
            osw.flush();
            osw.close();
            out.close();

            is = httpConn.getInputStream();
            rd = new BufferedReader(new InputStreamReader(is, "GBK"));

            System.out.println("测试类返回结果：");
            while ((xmlData = rd.readLine()) != null)
            {
                System.out.println(xmlData);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (rd != null)
            {

                try
                {
                    rd.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args)
    {
        new TestPay().send();
    }
}